﻿using System; 
using System.Data;
using System.Collections.Generic; 
using JOIN.Rights.Model;
using Kevin.Framework.Data;

/*------------------------------------------------------------------------
   苏州捷安信息科技有限公司 版权所有			
   文件名: TU_Function.cs
   作 者 : 席吉波
   日 期 : 2012-10-19
   备 注 : 当您对本文件更改时请写明修改人、修改日期以及事由
  ------------------------------------------------------------------------
*/

namespace JOIN.Rights.BLL
{
	 	//TU_Function
		/// <summary>
	/// Function 业务操作类
	/// </summary>
	public partial class FunctionBLL:BLLBase<FunctionInfo>,ISelect<FunctionInfo>
	{
		#region   构造函数
		
		/// <summary>
		/// FunctionBLL  构造函数  
		/// </summary>
		public FunctionBLL(FunctionInfo entity)
			:base(entity)
		{
		
		}
		/// <summary>
		/// FunctionBLL  构造函数  
		/// </summary>
		/// <param name="operationMode">操作模式</param>
        /// <param name="jsonEntity">前台javascript传过来的JSON实体</param>
		public FunctionBLL(string operationMode, string jsonEntity)
        {
            OperState cmd = (OperState)Enum.Parse(typeof(OperState), operationMode);
            FunctionInfo entity = ConvertJSON2Entity(jsonEntity);
            base.OperState = cmd;
            base.Entity = entity;
            base.ServiceResult = new ServiceResult<FunctionInfo>();
            base.ServiceResult.Successed = true;
            base.ServiceResult.Object = base.Entity;
        }
        
        #endregion
        
        #region 查询方法
        
        /// <summary>
        /// 获取 FunctionInfo 集合
        /// </summary>
        /// <param name="pageSize">分页记录数</param>
        /// <param name="pageIndex">页码索引</param>
        /// <returns></returns>
        public ServiceResult<FunctionInfo> SelectByProperties(int pageSize, int pageIndex)
        {
            this.ServiceResult = new ServiceResult<FunctionInfo>();
            try
            {
                int recordCount =0;
                List<FunctionInfo> list = base.Entity.SelectedByProperties<FunctionInfo>(out recordCount,pageSize,pageIndex);
                
                ServiceResult.Successed = true;
                ServiceResult.RecordCount = recordCount;
                ServiceResult.List = list;
                
            }
            catch(Exception ex){
                ServiceResult.ErrorMsg = "数据查询失败:"+ex.Message;
                ServiceResult.Successed = false;
                ServiceResult.RecordCount = 0;
                ServiceResult.List = null;
            }

            return ServiceResult;
        }
        
        #endregion
        
        #region 重写基类方法

        protected override bool PreSaveProcess()
        {
            if (OperState == BLL.OperState.Add)
            {
                base.Entity.ID = Guid.NewGuid().ToString();
            }
            return base.PreSaveProcess();
        }

        protected override void AfterSaveProcess()
        {
            if (OperState == BLL.OperState.Add)
            {
                RoleFunctionInfo rf = new RoleFunctionInfo();
                rf.FunctionID = base.Entity.FunctionID.ToString();
                rf.RoleID = string.IsNullOrEmpty(base.Entity.RoleID)?"0":base.Entity.RoleID;
                rf.Insert();
            }
        }

        protected override bool AfterDelete()
        {
            RoleFunctionInfo rf = new RoleFunctionInfo();
            rf.FunctionID = base.Entity.FunctionID.ToString();
            rf.RoleID = string.IsNullOrEmpty(base.Entity.RoleID) ? "0" : base.Entity.RoleID;
            rf.Delete(false);
            return base.AfterDelete();
        }
        
        #endregion
   
	}
}